package com.tanhua.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.api.db.UserInfoApi;
import com.tanhua.api.mongo.CommentApi;
import com.tanhua.api.mongo.PublishApi;
import com.tanhua.api.mongo.VideoApi;
import com.tanhua.db.UserInfo;
import com.tanhua.mongo.Comment;
import com.tanhua.mongo.Publish;
import com.tanhua.mongo.Video;
import com.tanhua.vo.*;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UsersService {

    @Reference
    private UserInfoApi userInfoApi;  //mybatisplus--mysql数据库

    @Reference
    private VideoApi videoApi; //mongoDB数据库

    @Reference
    private PublishApi publishApi;

    @Reference
    private CommentApi commentApi;



    public ResponseEntity findUsers(Integer page, Integer pagesize) {

        IPage<UserInfo> iPage=userInfoApi.findPage(page,pagesize);
        //组装页面需要的pageBeanVo
        PageBeanVo pageBeanVo = new PageBeanVo(page,pagesize,iPage.getTotal(),iPage.getRecords());
        return ResponseEntity.ok(pageBeanVo);
    }

    public ResponseEntity findById(Long userId) {
        UserInfo userInfo = userInfoApi.findById(userId);
        UserInfoVo vo=new UserInfoVo();
        BeanUtils.copyProperties(userInfo,vo);
        vo.setAge(userInfo.getAge()+"");
        vo.setCreated(userInfo.getCreated());
        return ResponseEntity.ok(vo);

    }


    public ResponseEntity findVideos(Integer page, Integer pagesize, Long userId) {

        PageBeanVo pageBeanVo=videoApi.findVideos(page,pagesize,userId);

        ArrayList<VideoVo> voList = new ArrayList<>();
        List<Video> items = (List<Video>) pageBeanVo.getItems();
        if(CollectionUtil.isNotEmpty(items)){
            for (Video video : items) {
                // 获取用户
                UserInfo userInfo = userInfoApi.findById(video.getUserId());
                VideoVo vo = new VideoVo();
                BeanUtils.copyProperties(userInfo,vo);
                BeanUtils.copyProperties(video,vo);
                vo.setId(video.getId().toString());
                vo.setCover(video.getPicUrl());
                vo.setSignature(video.getText());
                vo.setUserId(userInfo.getId());
                voList.add(vo);
            }
        }
        pageBeanVo.setItems(voList);
        return ResponseEntity.ok(pageBeanVo);
    }

    public ResponseEntity findMessages(Integer page, Integer pagesize, Long uid, Integer state) {

        //实现2个功能（用户动态  审核动态）
        PageBeanVo pageBeanVo=publishApi.findMessages(page,pagesize,uid,state);
        List<Publish> items = (List<Publish>) pageBeanVo.getItems();

        ArrayList<PublishVo> publishVoList = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(items)){
            for (Publish publish : items) {
                UserInfo userInfo = userInfoApi.findById(publish.getUserId());
                PublishVo vo = new PublishVo();
                vo.setUserInfo(userInfo);
                vo.setPublish(publish);
                vo.setCreateDate(new DateTime(publish.getCreated()).toString("yyyy-MM-dd HH:mm"));
                vo.setId(publish.getId().toString());
                publishVoList.add(vo);
            }
        }

        pageBeanVo.setItems(publishVoList);

        return ResponseEntity.ok(pageBeanVo);
    }

    public ResponseEntity findinfo(String publishId) {

        Publish publish = publishApi.findById(publishId);
        UserInfo userInfo=null;
        if(publish!=null){
            userInfo= userInfoApi.findById(publish.getUserId());
        }

        PublishVo vo = new PublishVo();
        vo.setUserInfo(userInfo);
        vo.setPublish(publish);
        vo.setId(publish.getId().toString());
        vo.setUserId(userInfo.getId());
        vo.setCreateDate(new DateTime(publish.getCreated()).toString("yyyy-MM-dd HH:mm"));
        return ResponseEntity.ok(vo);


    }

    public ResponseEntity findComments(Integer page, Integer pagesize, String publishId) {

        PageBeanVo pageBeanVo = commentApi.findInfo(page, pagesize, publishId);
        List<Comment> items = (List<Comment>) pageBeanVo.getItems();
        ArrayList<CommentVo> voList = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(items)){
            for (Comment comment : items) {
                UserInfo userInfo = userInfoApi.findById(comment.getUserId());
                CommentVo vo = new CommentVo();
                vo.setNickname(userInfo.getNickname());
                BeanUtils.copyProperties(comment,vo);
                vo.setCreateDate(new DateTime(comment.getCreated()).toString("yyyy-MM-dd HH:mm"));
                vo.setId(comment.getId().toString());
                voList.add(vo);
            }
        }

        pageBeanVo.setItems(voList);
        return ResponseEntity.ok(pageBeanVo);
    }
}
